package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Packaging;
import com.example.demo.entity.Test1;
import com.example.demo.mapper.PackagingMapper;
import com.example.demo.mapper.PackagingNewMapper;
import com.example.demo.mapper.TestMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pick")
public class PackagingController {
    @Resource
    PackagingMapper packagingMapper;
    @Resource
    PackagingNewMapper packagingNewMapper;

    @GetMapping("/send")
    public String findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "40") Integer pageSize,
                           @RequestParam(defaultValue = "MO01079293") String search) {
        LambdaQueryWrapper<Packaging> wrapper = Wrappers.<Packaging>lambdaQuery();
//        if (StrUtil.isNotBlank(search)){
//            wrapper.like(Packaging::get, search);
//        }
//        Page<Test1> testPage = testMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        return Result.success(testPage);
//            System.out.println(packagingMapper.getAll(search)[0]);
        List<Packaging> list = packagingMapper.getAll(search);
        System.out.println(list.get(0));
        return Result.success(list);
    }

    @PostMapping("/submit")
    public Result save(@RequestBody Packaging packaging) {
        QueryWrapper<Packaging> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("减速机成品序列号",packaging.get减速机成品序列号());
//        Packaging packaging1 = packagingNewMapper.selectOne(queryWrapper);

        List<Map<String, Object>> maps = packagingNewMapper.selectMaps(queryWrapper);
        if (maps.size() == 0){
            if (packaging.get写入请求次数() == null){
                packaging.set写入请求次数(1);
            }else{
                packaging.set写入请求次数(packaging.get写入请求次数()+1);
            }
            packagingNewMapper.insert(packaging);
        }else{
//            int i = (int) maps.get(0).get("写入请求次数");
            packaging.set写入请求次数((int) maps.get(0).get("写入请求次数")+1);
            packagingNewMapper.update(packaging,queryWrapper);
        }
//        System.out.println((int) maps.get(0).get("写入请求次数"));
//        if (packaging.get写入请求次数() == null){
//            packaging.set写入请求次数(1);
//        }else{
//            packaging.set写入请求次数(packaging.get写入请求次数()+1);
//        }
//        packagingNewMapper.insert(packaging);
        return Result.success();
    }

    @GetMapping("/history")
    public String history(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "40") Integer pageSize,
                          @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Packaging> wrapper = Wrappers.<Packaging>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Packaging::get生产订单号, search);
        }
        Page<Packaging> packagingPage = packagingNewMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(packagingPage);
//            System.out.println(packagingMapper.getAll(search)[0]);
//        List<Packaging> list = packagingMapper.getAll(search);
//        System.out.println(list.get(0));
//        return Result.success(list);
    }

//        @GetMapping("/submit")
//        public String submit(@RequestParam(defaultValue = "1") Integer pageNum,
//                            @RequestParam(defaultValue = "40") Integer pageSize,
//                            @RequestParam(defaultValue = "") String search){
//                LambdaQueryWrapper<Packaging> wrapper = Wrappers.<Packaging>lambdaQuery();
//        if (StrUtil.isNotBlank(search)){
//            wrapper.like(Packaging::get, search);
//        }
//        Page<Test1> testPage = testMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        return Result.success(testPage);
//            System.out.println(packagingMapper.getAll(search)[0]);
//                List<Packaging> list = packagingMapper.getAll(search);
//                System.out.println(list.get(0));
//                return Result.success(list);
//}

}
